
package formularios;

import clases.Estudiante;
import clases.Seccion;
import java.util.LinkedList;
import java.util.ListIterator;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class FormularioEstudiante extends javax.swing.JFrame {
    LinkedList<JTextField> textos;
    Seccion seccion;

    public FormularioEstudiante() {
        initComponents();
        textos = new LinkedList<JTextField>();
        textos.add(txtApellido);
        textos.add(txtCedula);
        textos.add(txtNombre);
    }

    public void setSeccion(Seccion seccion) {
        this.seccion = seccion;
    }

    

    public void limpiar(){
     ListIterator<JTextField> ite2=textos.listIterator();
        while (ite2.hasNext()){
            JTextField texto= ite2.next();
            texto.setText("");
        }
    }

    
    private void initComponents() {

        jPanel4 = new javax.swing.JPanel();
        btnCancelar = new javax.swing.JButton();
        btnSalir = new javax.swing.JButton();
        btnRetirar = new javax.swing.JButton();
        btnBuscar = new javax.swing.JButton();
        btnInscribir = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtCedula = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtApellido = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Inscripsion, Busqueda y Retiro de Estudiantes");
        getContentPane().setLayout(null);

        jPanel4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel4.setLayout(null);

        btnCancelar.setFont(new java.awt.Font("Arial", 0, 15)); 
        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });
        jPanel4.add(btnCancelar);
        btnCancelar.setBounds(340, 10, 100, 30);

        btnSalir.setFont(new java.awt.Font("Arial", 0, 15)); 
        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });
        jPanel4.add(btnSalir);
        btnSalir.setBounds(440, 10, 100, 30);

        btnRetirar.setFont(new java.awt.Font("Arial", 0, 15));
        btnRetirar.setText("Retirar");
        btnRetirar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRetirarActionPerformed(evt);
            }
        });
        jPanel4.add(btnRetirar);
        btnRetirar.setBounds(230, 10, 110, 30);

        btnBuscar.setFont(new java.awt.Font("Arial", 0, 15)); 
        btnBuscar.setText("Buscar");
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });
        jPanel4.add(btnBuscar);
        btnBuscar.setBounds(120, 10, 110, 30);

        btnInscribir.setFont(new java.awt.Font("Arial", 0, 15)); 
        btnInscribir.setText("Inscribir");
        btnInscribir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnInscribirActionPerformed(evt);
            }
        });
        jPanel4.add(btnInscribir);
        btnInscribir.setBounds(10, 10, 110, 30);

        getContentPane().add(jPanel4);
        jPanel4.setBounds(30, 200, 550, 60);

        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel2.setLayout(null);

        jLabel2.setFont(new java.awt.Font("Arial", 0, 15)); 
        jLabel2.setText("Nombre del Estudiante:");
        jPanel2.add(jLabel2);
        jLabel2.setBounds(30, 60, 200, 18);

        txtNombre.setFont(new java.awt.Font("Arial", 0, 15)); 
        jPanel2.add(txtNombre);
        txtNombre.setBounds(220, 60, 130, 28);

        jLabel3.setFont(new java.awt.Font("Arial", 0, 15)); 
        jLabel3.setText("Cedula del Estudiante:");
        jPanel2.add(jLabel3);
        jLabel3.setBounds(30, 20, 200, 18);

        txtCedula.setFont(new java.awt.Font("Arial", 0, 15)); 
        jPanel2.add(txtCedula);
        txtCedula.setBounds(220, 20, 130, 28);

        jLabel5.setFont(new java.awt.Font("Arial", 0, 15)); 
        jLabel5.setText("Apellido del Estudiante:");
        jPanel2.add(jLabel5);
        jLabel5.setBounds(30, 100, 200, 18);

        txtApellido.setFont(new java.awt.Font("Arial", 0, 15)); 
        jPanel2.add(txtApellido);
        txtApellido.setBounds(220, 100, 130, 28);

        getContentPane().add(jPanel2);
        jPanel2.setBounds(130, 30, 360, 150);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-612)/2, (screenSize.height-370)/2, 612, 370);
    }

    private void btnRetirarActionPerformed(java.awt.event.ActionEvent evt) {
       
         if(txtCedula.getText().equals("")){
                   JOptionPane.showMessageDialog(new JFrame(),"Existen campos vavios","Inscripsion, Busqueda y Retiro de Estudiantes",JOptionPane.WARNING_MESSAGE);
                   return;
        }
        Estudiante estu = seccion.BuscarEstudiante(txtCedula.getText());
        if(estu==null){
            JOptionPane.showMessageDialog(new JFrame(),"El estudiante no existe ","Inscripsion, Busqueda y Retiro de Estudiantes",JOptionPane.WARNING_MESSAGE);
            return;
        }
        else{
             seccion.RetirarEstudiante(txtCedula.getText());
             JOptionPane.showMessageDialog(new JFrame(),"El estudiante ha sido retirado ","Inscripsion, Busqueda y Retiro de Estudiantes",JOptionPane.INFORMATION_MESSAGE);
              limpiar();
        }
    }

    private void btnInscribirActionPerformed(java.awt.event.ActionEvent evt) {
        
          ListIterator<JTextField> ite2=textos.listIterator();
        while (ite2.hasNext()){
            JTextField texto= ite2.next();
            if(texto.getText().equals("")){
                   JOptionPane.showMessageDialog(new JFrame(),"Existen campos vavios","Inscripsion, Busqueda y Retiro de Estudiantes",JOptionPane.WARNING_MESSAGE);
                   return;
            }
        }
        Estudiante estu = seccion.BuscarEstudiante(txtCedula.getText());
        if(estu==null){
            seccion.InscribirEstudiante(new Estudiante(txtCedula.getText(), txtNombre.getText(), txtApellido.getText()));
            JOptionPane.showMessageDialog(new JFrame(),"El estudiante inscrito exitosamente","Inscripsion, Busqueda y Retiro de Estudiantes",JOptionPane.WARNING_MESSAGE);
            limpiar();
        }
        else{
                 JOptionPane.showMessageDialog(new JFrame(),"El Estudiante ya Existe","Inscripsion, Busqueda y Retiro de Estudiantes",JOptionPane.WARNING_MESSAGE);


        }
    }

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {
       
        if(txtCedula.getText().equals("")){
                   JOptionPane.showMessageDialog(new JFrame(),"Existen campos vavios","Inscripsion, Busqueda y Retiro de Estudiantes",JOptionPane.WARNING_MESSAGE);
                   return;
        }
        
        Estudiante estu = seccion.BuscarEstudiante(txtCedula.getText());
        if(estu==null){
            JOptionPane.showMessageDialog(new JFrame(),"El estudiante no existe ","Inscripsion, Busqueda y Retiro de Estudiantes",JOptionPane.WARNING_MESSAGE);
            return;
        }
        else{
             txtApellido.setText(estu.getApellido());
             txtCedula.setText(estu.getCedula());
             txtNombre.setText(estu.getNombre());
        }
    }

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {
        
        limpiar();
    }

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {
        
        dispose();
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FormularioEstudiante().setVisible(true);
            }
        });
    }

    
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnInscribir;
    private javax.swing.JButton btnRetirar;
    private javax.swing.JButton btnSalir;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JTextField txtApellido;
    private javax.swing.JTextField txtCedula;
    private javax.swing.JTextField txtNombre;
    

}
